package yja.gzw.app.activity

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityZhquxBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel

class ZhquxActivity :BaseActivity<Mainviewmodel, ActivityZhquxBinding>(R.layout.activity_zhqux){
    private lateinit var bottomSheetDialog: BottomSheetDialog
   var pingtais=""
    override fun initView() {
        val id = intent.getStringExtra("id")
        val youid = intent.getStringExtra("youid")

        binding.yonghua.setOnClickListener {

                    val intent=Intent(this, HaoyouzhuyeActivity::class.java)

            intent.putExtra("id",youid)
            startActivity(intent)

        }


        binding.qiangdan.setOnClickListener {

            //todo 抢单添加弹窗选择平台

            bottomSheetDialog = BottomSheetDialog(this)
            val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_pingtiaxuanze, null)
            bottomSheetDialog.setContentView(view)





            val xuan1 =view.findViewById<RadioButton>(R.id.xuan1)
            val xuan2 =view.findViewById<RadioButton>(R.id.xuan2)
            val xuan3 =view.findViewById<RadioButton>(R.id.xuan3)
            val xuan4 =view.findViewById<RadioButton>(R.id.xuan4)
            if(pingtais.contains("抖音")){
                xuan1.visibility= View.VISIBLE
            }
            if(pingtais.contains("快手")){
                xuan2.visibility= View.VISIBLE
            }
            if(pingtais.contains("西瓜视频")){
                xuan3.visibility= View.VISIBLE
            }
            if(pingtais.contains("bilibili")){
                xuan4.visibility= View.VISIBLE
            }




            val chacha =view.findViewById<ImageView>(R.id.chacha)
            val quxiao =view.findViewById<TextView>(R.id.quxiao)
            val queding =view.findViewById<TextView>(R.id.queding)
            chacha.setOnClickListener {
                bottomSheetDialog.dismiss()
            }

            quxiao.setOnClickListener {
                bottomSheetDialog.dismiss()
            }

            queding.setOnClickListener {
                   var name=""
                    if(xuan1.isChecked){
                        name="抖音"
                    }else if(xuan1.isChecked){
                        name="快手"

                    }else if(xuan1.isChecked){
                        name="西瓜视频"

                    }else{

                        name="bilibili"
                    }



                  viewmodel.onqiangdanwang(UserData.token,mapOf("order_id" to id!!,"platform" to name))

            }
            bottomSheetDialog.show()

        }
        binding.xiaoxi.setOnClickListener {
            val intent = Intent(this, TUIC2CChatActivity::class.java)
            intent.putExtra(TUIConstants.TUIChat.CHAT_ID,youid);
            intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            startActivity(intent)
        }




        viewmodel.onxiangqingwang(UserData.token,mapOf("order_id" to id!!))
    }
    override fun dataObservese() {

        viewmodel.onqiangdan.observe(this, Observer {
            if(it.code==1){
                finish()
                bottomSheetDialog.dismiss()
                Toast.makeText(this@ZhquxActivity,it.msg, Toast.LENGTH_SHORT).show()

            }else{
                Toast.makeText(this@ZhquxActivity,it.msg, Toast.LENGTH_SHORT).show()

            }


        })

        viewmodel.onxiangqing.observe(this, Observer {
            if(it.code==1){
                Log.e("3元专区的详情数据",it.data.toString())
                binding.biaoti.text=it.data.title
                binding.cishu.text="限制发布视频次数："+it.data.release_num

                binding. videoView.setUp(it.data.video_url.toString()
                    , "播放视频", Jzvd.SCREEN_WINDOW_NORMAL);
                binding. videoView.thumbImageView.setImageURI(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"));

                binding.pingtai.text=it.data.platform_id.toString()
                pingtais=it.data.platform_id.toString()
                binding.dingwei.text=it.data.address
                binding.shipinbiaoti.text=it.data.label
                binding.shipinneirun.text=it.data.content
                binding.huobiaoti.text=it.data.need_title
                if(it.data.type=="1"){
                    binding.spfabumoshi.text="自己使用Ai生成视频"

                }else if(it.data.type=="2"){

                    binding.spfabumoshi.text="第二方使用Ai生成的视频"
                }else{
                    binding.spfabumoshi.text="不使用Ai生成的视频"

                }

                binding.yonghumingcheng.text=it.data.publisher.username
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.publisher.avatar)
                    .apply(requestOptions)

                    .into(binding.touxiang)



            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }
        })

    }

}